﻿package view
{
    import cmd.*;
    import com.core.*;
    import com.core.display.*;
    import com.greensock.*;
    import com.sh.game.global.*;
    import control.*;
    import flash.display.*;
    import flash.events.*;
    import flash.filters.*;
    import flash.geom.*;
    import flash.text.*;
    import flash.utils.*;
    import manager.*;
    import morn.core.components.*;
    import morn.core.handlers.*;
    import plugin.taskdialog.consts.*;
    import pluginfw.nio.*;
    import view.message.*;
    import view.uilt.*;

    public class GuideViewPao extends Sprite
    {
        private var _tips:MyLabel;
        private var _zySprite:Sprite;
        private var lv:int;
        public var gid:int;
        private var _guideMc:Clip;
        private var isload:Boolean = false;
        private var _dir:int = 0;
        private var _noshow:int = 0;
        private var _item:Object;
        private var _tt:Number = 1;
        private var format2:TextFormat;
        private var guideFlag:Boolean;
        private var _byteLoader:Loader;
        private var _tweenLite1:TweenMax;
        private var _tweenLite2:TweenMax;
        private var timeoutID1:uint;
        private var timeoutID3:uint = 0;
        private var p5:Point;
        private var newguideObj:Object;
        private var p:Point;
        private var p1:Point;
        private var p2:Point;
        private var p3:Point;
        private var oldLevel:int = 0;
        private var fTaskArr:Array;
        private var timeoutID:uint;
        private var guideImg:Image;
        private var _cfgGuide:Object;
        private var _cfgTypeGuide:Object;
        private var _cfgLvGuide:Array;
        private var timeline:TweenMax;
        private var zyImg:Image;
        private var closeBtn:Button;
        private var isTxt:Boolean;
        private var square:Shape;
        private var _isshow:Boolean = true;
        private static var instance:GuideViewPao;

        public function GuideViewPao()
        {
            this.format2 = new TextFormat("宋体", 12, null, true);
            this.p5 = new Point();
            this.p = new Point(0, 0);
            this.p1 = new Point(0, 0);
            this.p2 = new Point(0, 0);
            this.p3 = new Point(0, 0);
            this.fTaskArr = [];
            this._cfgLvGuide = [];
            return;
        }// end function

        public function init() : void
        {
            this._zySprite = new Sprite();
            this.zyImg = new Image();
            this.zyImg.url = "png.common.guidezy";
            this.zyImg.sizeGrid = "40,40,50,20";
            this.zyImg.alpha = 0.8;
            this._zySprite.addChild(this.zyImg);
            this.zyImg.mouseEnabled = false;
            this.zyImg.mouseChildren = false;
            this.closeBtn = new Button("png.common.btn_close3");
            this._tips = new MyLabel();
            this._tips.leading = 3;
            this._tips.wordWrap = true;
            this._zySprite.addChild(this._tips);
            this._zySprite.addChild(this.closeBtn);
            this._zySprite.mouseEnabled = false;
            this.isload = true;
            if (this._cfgGuide == null)
            {
                this._cfgGuide = ConfigDictionary.data.newguide;
                this._cfgTypeGuide = ConfigDictionary.data.typenewguide;
            }
            this.closeBtn.clickHandler = new Handler(this.hide);
            var _loc_1:* = Control.create(GuideControl, this);
            ModelProxy.addObserver("guidepao", _loc_1);
            GlobalLayer.instance.getLayer("guideCon").addChild(this);
            this._tips.addEventListener(MouseEvent.MOUSE_DOWN, this.doStop);
            this.visible = false;
            this.mouseEnabled = false;
            return;
        }// end function

        protected function doStop(event:MouseEvent) : void
        {
            event.stopPropagation();
            return;
        }// end function

        private function linkHandler(event:TextEvent) : void
        {
            var nobj:Object;
            var e:* = event;
            var arr:* = e.text.split("&");
            switch(int(arr[0]))
            {
                case 1:
                {
                    nobj = ConfigDictionary.data.sysnpc[80];
                    if (this._isshow)
                    {
                        Alert2.show("即将瞬间传送到回收NPC处，是否确认传送？", "", ["确定", "取消"], function (param1:int, param2:Object) : void
            {
                if (param1 == 0)
                {
                    if (ItemUilt.checkMapIsFly())
                    {
                        PluginManager.instance.sendToServer("taskdialog", CMDTaskdialog.Fly, {x:nobj.x, y:nobj.y, tostageid:nobj.stageid});
                        ModelProxy.change("data", "taskfindpath", nobj.npcid);
                    }
                }
                else if (param1 == 100)
                {
                    if (ItemUilt.checkMapIsFly())
                    {
                        PluginManager.instance.sendToServer("taskdialog", CMDTaskdialog.Fly, {x:nobj.x, y:nobj.y, tostageid:nobj.stageid});
                        ModelProxy.change("data", "taskfindpath", nobj.npcid);
                    }
                    _isshow = false;
                }
                else if (param1 == 101)
                {
                    _isshow = false;
                }
                return;
            }// end function
            , null, false, true, true);
                    }
                    else if (ItemUilt.checkMapIsFly())
                    {
                        PluginManager.instance.sendToServer("taskdialog", CMDTaskdialog.Fly, {x:nobj.x, y:nobj.y, tostageid:nobj.stageid});
                        ModelProxy.change("data", "taskfindpath", nobj.npcid);
                    }
                    this.hide();
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        public function show(param1:String, param2:Point) : void
        {
            instance.visible = true;
            var _loc_3:int = 0;
            if (this._tweenLite1)
            {
                this._tweenLite1.kill();
            }
            if (this._tweenLite2)
            {
                this._tweenLite2.kill();
            }
            if (this.square && this.square.parent)
            {
                this.square.graphics.clear();
                this.square.filters = [];
                this.square.parent.removeChild(this.square);
            }
            if (this.timeline)
            {
                this.timeline.kill();
            }
            this._zySprite.x = 0;
            this._zySprite.y = 0;
            this.guideFlash();
            var _loc_4:* = this.newguideObj.w;
            var _loc_5:* = this.newguideObj.h;
            if (_loc_4 == 0 || _loc_5 == 0)
            {
                this.zyImg.setSize(220, 80);
            }
            else
            {
                this.zyImg.setSize(_loc_4, _loc_5);
            }
            if (this._dir == 1)
            {
                this.zyImg.rotation = 0;
                this._zySprite.x = -(this.zyImg.width - 14);
                this._zySprite.y = 3;
                this._tips.x = 15;
                this._tips.y = 35;
                this.closeBtn.x = this._tips.x + 182;
                this.closeBtn.y = this._tips.y;
            }
            else
            {
                this.zyImg.rotation = 180;
                this._zySprite.x = -14 + this.zyImg.width;
                this._zySprite.y = 3;
                this._tips.x = 8 - this.zyImg.width;
                this._tips.y = 8 - this.zyImg.height;
                this.closeBtn.x = this._tips.x + 190;
                this.closeBtn.y = this._tips.y;
            }
            this._tips.text = param1;
            this._tips.width = this.zyImg.width - 35;
            this._zySprite.visible = true;
            addChild(this._zySprite);
            if (this.timeoutID3)
            {
                clearInterval(this.timeoutID3);
                this.timeoutID3 = 0;
            }
            this.timeoutID3 = setInterval(this.onEnterFrame, 200);
            this._tips.removeEventListener(TextEvent.LINK, this.linkHandler);
            this._tips.addEventListener(TextEvent.LINK, this.linkHandler);
            return;
        }// end function

        private function guideFlash() : void
        {
            var _loc_1:String = null;
            var _loc_2:TextField = null;
            var _loc_3:String = null;
            var _loc_4:Label = null;
            var _loc_5:int = 0;
            var _loc_6:int = 0;
            if (this._item is Label || this._item is LinkButton)
            {
                this.isTxt = true;
                this.square = new Shape();
                this.square.graphics.lineStyle(1, 16766720);
                this.square.filters = [new GlowFilter(16580393, 0.8, 6, 6, 2, BitmapFilterQuality.LOW)];
                _loc_1 = "";
                if (this._item is Label)
                {
                    _loc_1 = this._item.text;
                }
                else
                {
                    _loc_1 = this._item.label;
                }
                _loc_2 = new TextField();
                _loc_2.htmlText = _loc_1;
                _loc_3 = _loc_2.text;
                _loc_4 = new Label();
                _loc_5 = _loc_3.indexOf(" ");
                if (_loc_5 > 0)
                {
                    _loc_4.text = _loc_3.substring(0, _loc_5);
                }
                else
                {
                    _loc_4.text = _loc_3;
                }
                _loc_4.autoSize = "left";
                _loc_6 = _loc_4.width;
                this.square.graphics.drawRect(this._item.x, this._item.y, _loc_6, 16);
                this.square.graphics.endFill();
                this._item.parent.addChild(this.square);
            }
            else
            {
                this.isTxt = false;
            }
            return;
        }// end function

        private function onEnterFrame() : void
        {
            this.p5 = this.getPoint();
            this.x = this.p5.x;
            this.y = this.p5.y;
            this.visible = true;
            return;
        }// end function

        public function hide(param1:Boolean = true) : void
        {
            instance.visible = false;
            this.visible = false;
            this.guideFlag = false;
            if (this.timeoutID)
            {
                clearTimeout(this.timeoutID);
                this.timeoutID = 0;
            }
            if (this.timeoutID1)
            {
                clearTimeout(this.timeoutID1);
                this.timeoutID1 = 0;
            }
            if (this.timeoutID3)
            {
                clearInterval(this.timeoutID3);
                this.timeoutID3 = 0;
            }
            if (this._tweenLite1)
            {
                this._tweenLite1.kill();
            }
            if (this._tweenLite2)
            {
                this._tweenLite2.kill();
            }
            if (this.timeline)
            {
                this.timeline.kill();
            }
            if (this._item)
            {
                this._item.filters = [];
            }
            if (this.square && this.square.parent)
            {
                this.square.graphics.clear();
                this.square.filters = [];
                this.square.parent.removeChild(this.square);
            }
            if (this._guideMc && this._guideMc.parent)
            {
                this._guideMc.parent.removeChild(this._guideMc);
                this._guideMc.dispose();
                this._guideMc = null;
            }
            return;
        }// end function

        public function doGuide(param1:Object, param2:Object) : void
        {
            var obj:* = param1;
            var _item1:* = param2;
            this.visible = false;
            if (this._item)
            {
                this._item.filters = [];
            }
            this._item = _item1;
            this._dir = obj.dir;
            this._noshow = obj.noshow;
            if (this.timeoutID)
            {
                clearTimeout(this.timeoutID);
                this.timeoutID = 0;
            }
            this.visible = false;
            this.newguideObj = obj;
            this.show(obj.desc, this.getPoint());
            if (obj.iszidong == 1)
            {
                this.timeoutID = setTimeout(this.clickHandler, int(obj.zidongtime) * 1000);
            }
            this.addGuide(obj.id);
            this.gid = obj.id;
            if (this.timeoutID1)
            {
                clearTimeout(this.timeoutID1);
                this.timeoutID1 = 0;
            }
            if (obj.showtime > 0)
            {
                this.timeoutID1 = setTimeout(function () : void
            {
                hide();
                return;
            }// end function
            , int(obj.showtime) * 1000);
            }
            return;
        }// end function

        private function clickHandler() : void
        {
            this._item.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
            return;
        }// end function

        private function doHs() : void
        {
            this.hide();
            PluginManager.instance.sendToBufferServer("zbhsy", CMDRecycle.RecycleFB, new Output());
            ModelProxy.change("data", "recycle", 1);
            return;
        }// end function

        private function getPoint() : Point
        {
            if (this._item == null)
            {
                this.hide(false);
                return this.p3;
            }
            if (this._item.hasOwnProperty("disabled") && this._item.disabled)
            {
                this.hide(false);
                return this.p3;
            }
            if (!this._item.parent || this._item.visible == false || !this._item.parent.parent && this._dir == 6 || !this._item.stage)
            {
                this.hide(false);
                return this.p3;
            }
            if (this._dir == 1)
            {
                this.p.x = int(this._item.width / 2);
                this.p.y = this._item.height;
            }
            else if (this._dir == 4)
            {
                this.p.x = 0;
                this.p.y = int(this._item.height / 2);
            }
            else if (this._dir == 2)
            {
                this.p.x = int(this._item.width / 2);
                this.p.y = 0;
            }
            else if (this._dir == 3)
            {
                this.p.x = this._item.width;
                this.p.y = int(this._item.height / 2);
            }
            this.p1 = this._item.localToGlobal(this.p3);
            if (this.p1.x == 40 && this.p1.y == 20 || this.p1.x == 40 && this.p1.y == 17 || this.p1.x == 24 && this.p1.y == 50 || this.p1.x == 160 && this.p1.y == 62)
            {
                this.hide(false);
                return this.p3;
            }
            this.p2.x = this.p1.x + this.p.x;
            this.p2.y = this.p1.y + this.p.y;
            if (this._item.rotation != 0)
            {
                this.p2.x = this.p1.x + this.p.x - this._item.width;
                this.p2.y = this.p1.y + this.p.y - this._item.height;
            }
            return this.p2;
        }// end function

        public function addGuide(param1:int) : void
        {
            var _loc_2:* = UserData.getOnlineData("guide");
            if (_loc_2 == null)
            {
                return;
            }
            if (int(_loc_2[param1]) == 0)
            {
                ModelProxy.change("plugin", "sendSysMsg", {name:"activity", param:param1, cmd:1001});
                _loc_2[param1] = 1;
            }
            return;
        }// end function

        public static function getInstance() : GuideViewPao
        {
            if (instance == null)
            {
                instance = new GuideViewPao;
            }
            return instance;
        }// end function

    }
}
